//
//  UIViewController.swift
//  fho
//
//  Created by 郭颢源 on 2021/4/8.
//

import Foundation
import UIKit
//MARK: - 点击文本框外收回键盘
extension UIViewController {
    
    /// 点击空白处结束编辑状态
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)
    }
    
    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
    ///
    /// Set navigation bar title color and size
    ///
    /// - parameter color: Title color
    /// - parameter font: Title font
    ///
    func sg_setNavigationBarTitle(color: UIColor, font: UIFont) {
        let dict = [NSAttributedString.Key.foregroundColor: color, NSAttributedString.Key.font: font]
        self.navigationController?.navigationBar.titleTextAttributes = dict
    }
    
    ///
    /// Return to the specified controller
    ///
    /// - parameter aClass: Specifies the controller class
    /// - parameter animated: Returns whether animation is required, default is true
    ///
    func sg_popToViewController(aClass: AnyClass, animated: Bool = true) {
        for vc in (self.navigationController?.viewControllers)! {
            if vc.isKind(of: aClass) {
                self.navigationController?.popToViewController(vc, animated: animated)
            }
        }
    }
    
    /**
     *  新手指引
     */
     
    func newUserGuide(_ view:UIView) {
        
        // 这里创建指引在这个视图在window上
        let frame:CGRect = UIScreen.main.bounds
            
        let bgView:UIView = UIView(frame: frame)
            
        bgView.backgroundColor = .black
        bgView.alpha = 0.8
        let tap = UITapGestureRecognizer(target: self, action: #selector(sureTapClick(_:)))
        bgView.addGestureRecognizer(tap)
//        UIApplication.shared.windows.first?.addSubview(self)
        UIApplication.shared.windows.first?.addSubview(bgView)
        
        
        //create path 重点来了（**这里需要添加第一个路径）
        let path:UIBezierPath = UIBezierPath(rect: frame)
        
        
        // 这里添加第一个路径 （这个是圆）
        let onePath = UIBezierPath(arcCenter: view.center, radius: 50, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: false)
        path.append(onePath)
        // 这里添加第二个路径 （这个是圆角矩形）
        path.append(UIBezierPath(roundedRect: CGRect(x: frame.size.width/2.0 - 1, y: 234, width: frame.size.width/2.0 + 1, height: 55), cornerRadius: 5).reversing())
        path.addArc(withCenter: CGPoint(x:300,y:150), radius: 100, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: false)
        
        // 这里添加第三个路径 （这个是矩形）
        path.append(UIBezierPath(rect: CGRect(x: 20, y: 20, width: 99, height: 99)).reversing())
        // reversing 的意思就是反转 就是将你设置的路径以外的部分加入到路径中这样 你设置的路径就不在绘制的范围之内。在setMaskLayer的时候 你设置的那部分路径就没有了 就成了镂空的。 clockwise 这个值得意思也是这样。

        let shapeLayer:CAShapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = UIColor.blue.cgColor
        shapeLayer.backgroundColor = UIColor.systemBlue.cgColor
        bgView.layer.mask = shapeLayer
        
        let imageView:UIImageView = UIImageView(frame: CGRect(x: frame.size.width - 300, y: 72, width: 60, height: 60))
            
        imageView.image = UIImage(named: "zhongtong")
        bgView.addSubview(imageView)
        
    }
    /**
     *   新手指引确定
     */
    @objc func sureTapClick(_ tap:UITapGestureRecognizer)
    {
        
        let view:UIView = tap.view!
        view.removeFromSuperview()
        view.removeGestureRecognizer(tap)
        
//        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstCouponBoard_iPhone"]
    }
}
